package com.example.mahao_final.UI

import android.os.Bundle
import android.widget.RatingBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.mahao_final.R
import com.example.mahao_final.database.BlogDatabase
import kotlinx.coroutines.launch

class BlogDetailActivity : AppCompatActivity() {

    private val blogDao by lazy { BlogDatabase.getDatabase(this).blogDao() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_blog_detail)

        // 获取传递过来的博客 ID
        val blogId = intent.getLongExtra("BLOG_ID", 0)

        // 从数据库获取博客详细信息
        lifecycleScope.launch {
            val blog = blogDao.getBlogById(blogId)  // 从数据库获取博客内容

            if (blog != null) {
                // 显示博客的详细信息
                findViewById<TextView>(R.id.tv_title).text = blog.title
                findViewById<TextView>(R.id.tv_description).text = blog.description
                findViewById<TextView>(R.id.tv_author).text = "作者: ${blog.author}"

                // 设置难度星星
                findViewById<RatingBar>(R.id.rating_bar_difficulty).rating = blog.difficulty

                findViewById<TextView>(R.id.tv_tags).text = "标签: ${blog.tags.joinToString(", ")}"
                findViewById<TextView>(R.id.tv_content).text = blog.content  // 显示博客内容
            }
        }
    }
}
